package com.visikard.ui.activity;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

import com.visikard.application.UIMessage;
import com.visikard.application.VKRegApplication;
import com.visikard.asynctask.BaseTask;
import com.visikard.asynctask.RingUpTask;
import com.visikard.item.ItemGiftKardRedeem;
import com.visikard.ui.R;
import com.visikard.utility.BitmapUtils;
import com.visikard.utility.UILImageUtils;
import com.visikard.utility.Utils;
import com.visikard.widget.PuchRatingBar;

public class PunchRedeemActitivy extends BaseLoginCompleteActivity {

	public static final String EXT_GIFT_KARD_ITEM = "extrasGiftKarditem";

	private ItemGiftKardRedeem mGiftKard;

	public static final String EXTRA_ITEM_PUNCH = "WalletPunchDetail_extra_item_punch";
	public static final String EXTRA_ITEM_PUNCH_NEW = "WalletPunchDetail_extra_item_punch_new";
	public static final String EXTRA_ITEM_PUNCH_TAP = "WalletPunchDetail_extra_item_punch_tap";

	private static final int MAX_STARS_PER_LINE = 5;

	private ImageView mPunchImage;
	private TextView mPunchName, mPunchDetail;
	private PuchRatingBar mPunchRating;
	private TextView txtSuccess, txtPunchRecorded, txtGiftKard;
	private ImageView imageBack;
	private Button btnFinish;
	//
	private Dialog mDialog;

	private BaseTask mAsyncTask;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.wallet_punch_detail);
		mPunchImage = (ImageView) findViewById(R.id.punch_detail_kard_image);
		mPunchName = (TextView) findViewById(R.id.punch_detail_info_kard_name);
		mPunchDetail = (TextView) findViewById(R.id.punch_detail_info_kard_detail);
		mPunchRating = (PuchRatingBar) findViewById(R.id.punch_detail_rating);
		mGiftKard = (ItemGiftKardRedeem) getIntent().getSerializableExtra(EXT_GIFT_KARD_ITEM);
		int maxLines = mGiftKard.getRequireBuy() % MAX_STARS_PER_LINE > 0 ? mGiftKard.getRequireBuy()
				/ MAX_STARS_PER_LINE + 1 : mGiftKard.getRequireBuy() / MAX_STARS_PER_LINE;
		mPunchRating.setMaxLines(maxLines);
		LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
		params.topMargin = (int) (maxLines * getResources().getDimension(R.dimen.top_menu_bar_height));

		// ImageLoader.getInstance(this).loadImageCorner(
		// mPunchImage, mGiftKard.getImgCard(),
		// 0, BitmapUtils.ROUND_NONE, 0, 0, 0, 0, 0);
		UILImageUtils.loadImage(mPunchImage, // image view
				mGiftKard.getImgCard(), // url
				0, // size of view
				BitmapUtils.ROUND_NONE, // round all
				0, // round size
				0, // shadow
				0, // shadow resource
				0, 0);
		// UILImageUtils.loadImage(mPunchCategoryImage, mGiftKard.get, 0, BitmapUtils.ROUND_NONE, 0, 0, 0, 0, 0);
		mPunchName.setText(mGiftKard.getMerchantName());
		mPunchDetail.setText(mGiftKard.getCampaignDetail());
		mPunchRating.setRating(0, mGiftKard.getRequireBuy());

		imageBack = (ImageView) findViewById(R.id.punch_detail_menu_back);
		imageBack.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});

		btnFinish = (Button) findViewById(R.id.btn_finish);
		btnFinish.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});

		txtSuccess = (TextView) findViewById(R.id.txt_sucess);
		txtPunchRecorded = (TextView) findViewById(R.id.txt_punch_recorded);
		txtGiftKard = (TextView) findViewById(R.id.txt_giftkard);

		mAsyncTask = new RingUpTask(mHandler);

		// redeem right away
		mAsyncTask.execute(VKRegApplication.uid, 0L, mGiftKard.getGiftKardUserId(), mGiftKard.getIdTransaction(), 0L,
				"0", "0", mGiftKard.getIsGiftKard(), "");
	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			finish();
			return true;
		}
		else {
			return super.onKeyDown(keyCode, event);
		}
	}

	// Handle message method
	@Override
	protected boolean handleMessage(Message msg) {
		switch (msg.what) {
			case UIMessage.MSG_RINGUP_SUCCESS:
				txtSuccess.setVisibility(View.VISIBLE);
				int punchRecorded = 0;
				Bundle data = msg.getData();
				if (data != null)
					punchRecorded = data.getInt(RingUpTask.KEY_PUNCH_RECORDED);
				if (punchRecorded > 0) {
					String strPunchRecorded = String.format(getString(R.string.txt_punch_kard_punch_recorded),
							Utils.ordinal(punchRecorded));
					txtPunchRecorded.setText(strPunchRecorded);
					txtPunchRecorded.setVisibility(View.VISIBLE);
					if (punchRecorded >= mGiftKard.getRequireBuy()) {
						String strGiftKard = String.format(getString(R.string.txt_giftcard_has_been_sent),
								mGiftKard.getPurchase_value());
						txtGiftKard.setText(strGiftKard);
						txtGiftKard.setVisibility(View.VISIBLE);
					}
					mPunchRating.setRating(punchRecorded, mGiftKard.getRequireBuy());
					mPunchRating.setAnimation(true);
				}
				return true;
			case UIMessage.MSG_RINGUP_FAILED:
				mDialog = new AlertDialog.Builder(PunchRedeemActitivy.this).setIcon(android.R.drawable.ic_dialog_info)
						.setMessage(R.string.ringup_failed)
						.setPositiveButton(R.string.literal_ok, new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog, int which) {
								dialog.cancel();
								finish();
							}
						}).create();
				mDialog.show();
				return true;
			case UIMessage.MSG_RINGUP_EXPIRED:
				mDialog = new AlertDialog.Builder(PunchRedeemActitivy.this).setIcon(android.R.drawable.ic_dialog_info)
						.setMessage(R.string.ringup_expired)
						.setPositiveButton(R.string.literal_ok, new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog, int which) {
								dialog.cancel();
								finish();
							}
						}).create();
				mDialog.show();
				return true;
		}
		return super.handleMessage(msg);
	}

}
